package com.wanpinghui.supplier.product.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.product.interfaces.SelectFormatListener;
import com.wanpinghui.supplier.widgets.PlusMinuesView;
import com.wanpinghui.wphlibrary.model.product.bean.ProductFormatBean;
import com.yunshl.yunshllibrary.utils.NumberUtil;

import java.util.List;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/12/21
 ***/

public class SelectFormatAdapter extends BaseAdapter {
    private Context context;
    private List<ProductFormatBean> datas;
    private SelectFormatListener listener;

    public void setListener(SelectFormatListener listener) {
        this.listener = listener;
    }

    public void setDatas(List<ProductFormatBean> list) {
        datas = list;
        notifyDataSetChanged();
    }

    public int getAllCount() {
        int count = 0;
        if (getCount() > 0) {
            for (ProductFormatBean bean : datas) {
                count = count + bean.getCount();
            }
        }
        return count;
    }

    public double getAllTotal() {
        double total = 0;
        if (getCount() > 0) {
            for (ProductFormatBean bean : datas) {
                if (bean.getCount() > 0 && bean.isIs_consult()) {
                    return -1;
                } else {
                    total = total + (bean.getPrice() * bean.getCount());
                }
            }
        }
        return total;
    }

    public SelectFormatAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_select_format, null);
        TextView textViewName = (TextView) view.findViewById(R.id.tv_name);
        TextView textViewPrice = (TextView) view.findViewById(R.id.tv_price);
        PlusMinuesView plusMinuesView = (PlusMinuesView) view.findViewById(R.id.view_plus_minues);
        final ProductFormatBean bean = datas.get(position);
        textViewName.setText(bean.getName());
        if (bean.isIs_consult()) {
            textViewPrice.setText("议价");
        } else {
            textViewPrice.setText("￥" + NumberUtil.double2String(bean.getPrice()));
        }
        plusMinuesView.setCount(bean.getCount());
        plusMinuesView.setOnCountChangeListener(new PlusMinuesView.OnCountChangeListener() {
            @Override
            public void onPlus() {
            }

            @Override
            public void onMinues() {
            }

            @Override
            public void onCountChange(int count) {
                bean.setCount(count);
                if (listener != null) {
                    listener.onFormatChange(getAllCount(), getAllTotal());
                }
            }
        });

        return view;
    }


}
